package com.alibaba.citrus.cr.unified.inventory.utils;

import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.lattice2.epoch.db.service.CustomSequenceAccessor;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateOrderTypeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: 生成code 规则 STO + "调拨类型" + yyyyMMDD + 8位数字
 * @author: yuhw
 * @date: 2021年11月20日 15:15
 **/
public class CodeUtil {
    private static CustomSequenceAccessor customSequenceAccessor;
    static {
        customSequenceAccessor = SpringContextUtil.getBean(CustomSequenceAccessor.class);
    }
//    private static final String APP_CODE = "ots_unified_inventory";
    private static final String APP_CODE = "cr_unified_inventory";
    private static final String SEQ_CODE = "ALLOCATE_ORDER";
    private static final String PSTO_CODE = "ALLOCATE_APPLY_ORDER";
    private static final String PREFIX = "STO";
    private static final String PSTO = "PSTO";

    private static String getSeq(){
        String seq = customSequenceAccessor.getNextStringSequence(APP_CODE,SEQ_CODE);
        return seq;
    }

    public static String planCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX  + System.currentTimeMillis();
        }
        return PREFIX + "1" + seq;
    }
    public static String salesCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + "2" + seq;
    }

    public static String sameCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + "3" + seq;
    }
    public static String crossCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + "4" + seq;
    }

    public static String betweenCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + "5" + seq;
    }

    public static String freightCode(){
        String seq = getSeq();
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + "6" + seq;
    }



    public static String getCode(Integer type){
        if (type == null){
            return null;
        }
        if (AllocateOrderTypeEnum.PLAN.getType().intValue() == type.intValue()){
            return planCode();
        }else if (AllocateOrderTypeEnum.HEADQUARTER_SALES.getType().intValue() == type.intValue()){
            return salesCode();
        }else if (AllocateOrderTypeEnum.SAME_COMPANY.getType().intValue() == type.intValue()){
            return sameCode();
        }else if (AllocateOrderTypeEnum.CROSS_COMPANY.getType().intValue() == type.intValue()){
            return crossCode();
        } else if (AllocateOrderTypeEnum.BETWEEN_WAREHOUSE.getType().equals(type)) {
            return betweenCode();
        } else if (AllocateOrderTypeEnum.FREIGHT_YARD_PICKUP.getType().equals(type)) {
            return freightCode();
        }
        return null;
    }

    public static String getCode(){
        String seq = customSequenceAccessor.getNextStringSequence(APP_CODE,SEQ_CODE);
        if (StringUtils.isBlank(seq)){
            return PREFIX + System.currentTimeMillis();
        }
        return PREFIX + seq;
    }

    public static String getPSTOCode(){
        String seq = customSequenceAccessor.getNextStringSequence(APP_CODE,PSTO_CODE);
        if (StringUtils.isBlank(seq)){
            return PSTO + System.currentTimeMillis();
        }
        return PSTO + seq;
    }

}
